package com.tusuo.stds.ui.level;

import android.content.Context;
import android.widget.Toast;

import com.hannesdorfmann.mosby.mvp.MvpBasePresenter;
import com.tusuo.stds.MyApp;
import com.tusuo.stds.R;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.LevelRecordList;
import com.tusuo.stds.util.Utils;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;
import timber.log.Timber;

/**
 * @author J.Huang
 * @version 1.0
 */
@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(LevelRecordPresenter.class)
class LevelRecordPresenter extends MvpBasePresenter<LevelRecordView> {

    @Inject
    ApiHelper mApiHelper;

    private Context mContext;
    private String mErrMsgServer;
    private int pageNo = 1;
    private static final int pageSize = 10;

    private Subscriber<LevelRecordList> mGetLevelRecordListSubscriber = null;

    LevelRecordPresenter(Context context) {
        mContext = context;
        mErrMsgServer = mContext.getResources().getString(R.string.err_msg_server);

        Injector.initLevelRecordPresenterComponent().inject(this);
    }

    @Override
    public void attachView(LevelRecordView view) {
        super.attachView(view);

        mGetLevelRecordListSubscriber = new GetLevelRecordListSubscriber();

        mApiHelper.getLevelRecordList(mGetLevelRecordListSubscriber, pageNo, pageSize);

    }

    @Override
    public void detachView(boolean retainInstance) {
        if (mGetLevelRecordListSubscriber != null) {
            mGetLevelRecordListSubscriber.unsubscribe();
            mGetLevelRecordListSubscriber = null;
        }
        super.detachView(retainInstance);
    }

    public void getRvData() {
        mApiHelper.getLevelRecordList(new GetLevelRecordListSubscriber(), pageNo, pageSize);
    }

    private class GetLevelRecordListSubscriber extends Subscriber<LevelRecordList> {
        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
            Timber.d(e.toString());
            Utils.UI.showToastShort(mErrMsgServer);
        }

        @Override
        public void onNext(final LevelRecordList list) {


            if (list.checkState(mContext) && getView() != null) {
                if (list.Total < 1 || list.Data.size() < 1) {
                    if (pageNo == 1) {
                        getView().showNoList();
                    } else {
                        getView().hintLoadMore();
                    }

                } else {
                    getView().updateRvData(list.Data);
                    if (pageNo == 1) getView().scrollRvToFirst();
                    pageNo++;
                }

//                   if (list.Total < 1 || list.Data.size() < 1) {
//                    getView().showNoList();
//                } else {
//                getView().setListViewAdapter(new LevelRecordListAdapter(mContext, list.Data));
//                }
            }
        }
    }
}
